package practice1_100;

import java.util.*;

public class Practice451 {
    public String frequencySort(String s) {
        char[] chars=s.toCharArray();
        Map<Character,Integer> treeMap=new TreeMap<Character,Integer>();
        //统计每一个字符出现的次数，map
        for(char c:chars){
            treeMap.put(c,treeMap.getOrDefault(c,0)+1);
        }
        //将map转成一个set集合，用List存储每一条（KV）
        List<Map.Entry<Character,Integer>> list = new ArrayList<Map.Entry<Character,Integer>>(treeMap.entrySet());
        //对list排序，根据后面Integer排序
        Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
            @Override//降序排序
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                return o2.getValue()-o1.getValue();
            }
        });

        StringBuffer stringBuffer=new StringBuffer();
        //排完序直接
        for (int i=0;i<list.size();++i) {
            int res=list.get(i).getValue();
            for (int j = 0; j < res; j++) {
                stringBuffer.append(list.get(i).getKey());
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
     String a=   new Practice451().frequencySort("faifiqgrjkgvnerkabjnsilkbaihubadbvdsavbbfaulwaufhiilvbveiabvilabgilbviaeugiubvuebagebrubviue");
        System.out.println(a);
    }
}
